import pandas as pd
import yaml

from yaml import SafeLoader

from datareport.api import ImageUtil
from datareport.api.DataSource import DataSource
from datareport.api.annotation.Font import Font
from datareport.impl.paragraph.base.BaseParagraph import BaseParagraph
from datareport.api.annotation.Style import style


@style()
class KaiZhanQingKuang_one_zhengti_34(BaseParagraph):
    """

    """

    def __init__(self):
        super().__init__()
        self.text = '''1、整体情况:根据统计结果，{}月的完成度较高，为{}%;{}月最低，为{}%。'''
        self.sort = 24
        self.section = 9

    def plot(self, df, plt):
        pass

    def getData(self, result):
        pass

    def start(self, conn, year, plt):
        plan=pd.read_sql('''select p.*,r.num num1 from (
        select convert(month,char) month, count(*) num from t_meetingplan  where isPlan=1 and completeStatus='通过'
        group by month) p left join (select month(createTime) month,count(1) num from t_meetinginfo where completeStatus='通过'
        group by month(createTime)) r on p.month=r.month
        ''',con=conn)
        plan=plan.fillna(0)
        plan['percent']=round(100*plan['num1']/plan['num'],1)
        plan.sort_values('percent',ascending=False,inplace=True)
        self.text=self.text.format(plan.iloc[0].at['month'],
                                   plan.iloc[0].at['percent'],
                                   plan.iloc[len(plan)-1].at['month'],
                                   plan.iloc[len(plan)-1].at['percent'])
        self.fonts.append(Font(text=self.text))
        shiji=pd.read_sql('''select partyBranchName '基层组织',count(1) '实际活动量' 
        from(select * from t_meetinginfo where completeStatus='通过')new 
        group by partyBranchName
        ''',con=conn)
        jihua = pd.read_sql('''select partyBranchName '基层组织',count(1) '计划活动量' 
                from(select * from t_meetingplan where completeStatus='通过')new
                group by partyBranchName
                ''', con=conn)
        shiji_zt = pd.read_sql('''select partyBranchName '基层组织',type '专题',
        count(1) '实际活动量' from(select partyBranchName ,
        (select name from t_podict where value=t_meetinginfo.type) type from t_meetinginfo where completeStatus='通过')new
        group by partyBranchName,type
                ''', con=conn)
        jihua_zt = pd.read_sql('''select partyBranchName '基层组织',type '专题',
                count(1) '计划活动量' from(select partyBranchName,
                (select name from t_podict where value=t_meetingplan.type) type from t_meetingplan where completeStatus='通过')new
                group by partyBranchName,type
                        ''', con=conn)
        self.tables.append(pd.merge(shiji, jihua, on='基层组织'))
        self.tables.append(shiji_zt.merge(jihua_zt, on=['基层组织', '专题'], how='outer').fillna(0))
        plan['month']=plan['month'].values+'月'
        self.image=ImageUtil.line(plan['month'].values.tolist(),plan['percent'].values.tolist(),'百分比（%）')

if __name__ == '__main__':
    with open('D:\work\sanhuiyike\datareport\config.yaml', encoding='utf-8') as f:
        data = yaml.load(f, Loader=SafeLoader)
    con=DataSource(data['datasource']).conn
    KaiZhanQingKuang_one_zhengti_34().start(con,2023,'')
